<?php
/**
 * From here we build the website(client),
 * that a player / visitor will see and use
 * to play the game.
 *
 * @author Mark Dohmen.
 */
final class Client {
	
	/**
	 * Instance of this class.
	 */
	private static $client = NULL;
	
	/**
	 * Instance of Smarty Template Parser.
	 */
	private $smarty = NULL;
	
	/**
	 * Instance of ModuleManager.
	 */
	private $moduleManager = NULL;
	
	/**
	 * Class Constructor..
	 */
	final public function Client() {
		$this->smarty = new Smarty();
		try {
			$this->moduleManager = new ModuleManager();
		} catch(ClientException $e) {
			$e->createError()->showError();
		}
	}
	
	/**
	 * Initialize the client.
	 */
	final public function init(string $module) {
		try {
			$this->moduleManager->load();
			if (Login::getLogin()->checkUserLoggedIn()) {
				World::init(Login::getLogin()->getWorldId());
			} else {
				$this->moduleManager->getModuleByName($module);	
			}
		} catch(ClientException $e) {
			$e->createError()->showError();
		}
	}
	
	/**
	 * Returns an instance of Client.
	 *
	 * @return $client
	 */
	final public static function getClient() {
		if (!(self::$client instanceof Client)) {
			self::$client = new Client();
		}
		return (object) self::$client;
	}
	
	/**
	 * Return an instance of Smarty.
	 *
	 * @return $smarty
	 */
	final public function getSmarty() {
		if ($this->smarty instanceof Smarty) {
			return (object) $this->smarty;
		}
		return NULL;
	}
	
	/**
	 * Returns an instance of ModuleManager.
	 *
	 * @return $moduleManager
	 */
	final public function getModuleManager() {
		if ($this->moduleManager instanceof ModuleManager) {
			return (object) $this->moduleManager;
		}
		return NULL;
	}
}
?>